package com.accelstack.cmp.controller;

import com.accelstack.cmp.security.SessionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Session管理控制器
 */
@Tag(name = "Session管理", description = "Session相关接口")
@RestController
@RequestMapping("/api/session")
@RequiredArgsConstructor
public class SessionController {
    
    private final SessionService sessionService;
    
    @Operation(summary = "获取Session信息", description = "获取当前Session的详细信息")
    @GetMapping("/info")
    public ResponseEntity<Map<String, Object>> getSessionInfo(HttpServletRequest request) {
        return ResponseEntity.ok(sessionService.getSessionInfo(request));
    }
    
    @Operation(summary = "刷新Session", description = "延长Session有效期")
    @PostMapping("/refresh")
    public ResponseEntity<String> refreshSession(HttpServletRequest request) {
        sessionService.refreshSession(request);
        return ResponseEntity.ok("Session已刷新");
    }
    
    @Operation(summary = "登出", description = "销毁当前Session")
    @PostMapping("/logout")
    public ResponseEntity<String> logout(HttpServletRequest request) {
        sessionService.destroySession(request);
        return ResponseEntity.ok("登出成功");
    }
    
    @Operation(summary = "在线用户数", description = "获取当前在线用户数量")
    @GetMapping("/online-count")
    public ResponseEntity<Map<String, Object>> getOnlineCount() {
        long count = sessionService.getOnlineUserCount();
        return ResponseEntity.ok(Map.of("onlineUserCount", count));
    }
}

